package serializer

import (
	"singo/model"
	"strings"
	"time"
)

// Defect 序列化器
type Defect struct {
	UID        string        `json:"id"`
	Name       string        `json:"name"`
	Intro      string        `json:"intro"`
	Status     uint          `json:"status"`
	Priority   uint          `json:"priority"`
	Progress   int           `json:"progress"`
	CreatedBy  interface{}   `json:"created_by"`
	ExecuteBy  []interface{} `json:"execute_by"`
	Project    interface{}   `json:"project"`
	CreatedAt  string        `json:"created_at"`
	StartAt    string        `json:"start_at"`
	DeadlineAt string        `json:"deadline_at"`
	CompleteAt string        `json:"complete_at"`
}

// BuildDefect 序列化
func BuildDefect(item model.Defect) Defect {
	var executeUsers []interface{}
	executeArr := strings.Split(item.ExecuteBy, ",")
	for _, item := range executeArr {
		executeUsers = append(executeUsers, BuildUserSimple(item))
	}

	return Defect{
		UID:        item.UID,
		Name:       item.Name,
		Intro:      item.Intro,
		Status:     item.Status,
		Priority:   item.Priority,
		Project:    BuildProjectSimple(item.ProjectID),
		Progress:   item.Progress,
		CreatedBy:  BuildUserSimple(item.CreatedBy),
		ExecuteBy:  executeUsers,
		CreatedAt:  item.CreatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		StartAt:    serializerTime(item.StartAt),
		DeadlineAt: serializerTime(item.DeadlineAt),
		CompleteAt: serializerTime(item.CompleteAt),
	}
}

// BuildDefects 序列化列表
func BuildDefects(items []model.Defect) (demands []Defect) {
	for _, item := range items {
		demand := BuildDefect(item)
		demands = append(demands, demand)
	}
	return demands
}
